#include "openmove_Prefix.h"
#include "SceneBase.h"
#include "OgreEngine.h"
#include "ExampleFrameListener.h"
#include "SkeletalAnimationFrameListener.h"
#include "OgreExternalTextureSourceManager.h"
#include "OgreExternalTextureSource.h"
#include "SceneSouffle.h"
#include "SimpleFrameListener.h"

CSceneSouffle :: CSceneSouffle(COgreEngine &Creator, const String & Name, const int pos) : CSceneBase(Creator, Name, pos), m_SignTime(0), m_Play(false), m_CurrentSpeed(0)
{
}

CSceneSouffle :: ~CSceneSouffle(void)
{
}

void CSceneSouffle :: Create(TCreatorEntities const &CreatorEntities, TCreatorNodes const &CreatorNodes, TCreatorLights const &CreatorLights, ENetConfig IsClient, Vector3 &CameraStartPos)
{
	CSceneBase::Create(CreatorEntities, CreatorNodes, CreatorLights, IsClient, CameraStartPos);
}

void CSceneSouffle :: Attach(void)
{
	CSceneBase::Attach();
	//m_pCurrentCamera->setPosition(-180.8f, 87.7f, 452.3f);
	//Quaternion Quat(Radian(Degree(0.0f)), Vector3(0, 1, 0));
	//m_pCurrentCamera->setOrientation(Quat);
	//m_pCurrentCamera->setDirection(0, 0, -1);
	//m_OgreEngine.m_pViewPort->setBackgroundColour(ColourValue(1.0f, 1.0f, 1.0f, 1.0f));
	//m_OgreEngine.m_pCurrentSceneManager->setSkyBox(true, "SkyBox/Souffle");
	m_SignTime = 0;
	m_Play = false;
	m_lastTime = m_Timer.getSeconds();
}

bool CSceneSouffle :: frameStarted(const FrameEvent& evt)
{
	CSceneBase::frameStarted(evt);

	if (m_pCameraFrameListener->mInputDevice->isKeyDown(KC_Z))
	{
		fakeAddEvent(SOpenMoveDisplayPacket::e_SE_MoveSign, 0.3);
	} 

	Real curTime = m_Timer.getSeconds();
	if (m_Play)
	{
		m_SignTime += (curTime - m_lastTime);
		m_CurrentSpeed = std::min(m_CurrentSpeed + 0.05f, 1.0f);
	}
	else
	{
		m_SignTime += (curTime - m_lastTime) * m_CurrentSpeed;
		m_CurrentSpeed = std::max(m_CurrentSpeed - 0.05f, 0.0f);
	}

	if (m_NetConfig == e_NCNone || m_NetConfig == e_NCServer)
	{
		Entity *ent = findEntity(String("signes"));
		if (NULL != ent && ent->hasSkeleton())
		{
			AnimationState *state = ent->getAnimationState("signes");
			if (NULL != state)
			{
				state->setTimePosition(m_SignTime);
				state->setLoop(false);
			}
		}
	}

	m_OgreEngine.setDebugText("signTime: " + StringConverter::toString(m_SignTime));

	if (0)
	{
		SceneNode *nodeClone = (SceneNode *)m_pSceneNode->getChild("couple-soufle-ana");
		Entity *entClone = findEntity(String("couple-soufle-ana"));
		if (NULL != entClone && NULL != nodeClone)
		{
			Vector3 distVec = ((nodeClone->getPosition() + entClone->getSkeleton()->getBone(0)->getPosition()) - m_pCurrentCamera->getPosition());
			Real dist = distVec.length();
			dist += 150.0f;
			m_OgreEngine.m_pCurrentSceneManager->setShadowFarDistance(dist);
		}
	}

	Light *light = findLight(String("Souffle-spotLight1"));
	if (NULL != light)
	{
		light->setSpotlightOuterAngle(Degree(60));
		if ((!m_Timer.m_UseManualTime && m_Timer.getSeconds() > 160.0f) || (m_Timer.m_UseManualTime && m_Timer.m_ManualTime > 160000.0f))
		{
			light->setDiffuseColour(ColourValue::White);
		}
		else
		{
			light->setDiffuseColour(ColourValue::Black);
		}
	}

	//if ((!m_UseManualTime && m_Timer.getSeconds() > 30.0f) || (m_UseManualTime && m_ManualTime > 30000.0f))
	//{
	//	m_OgreEngine.m_pViewPort->setShadowsEnabled(true);
	//}
	//else
	//{
	//	m_OgreEngine.m_pViewPort->setShadowsEnabled(false);
	//}

	MaterialPtr solTxt = MaterialManager::getSingleton().getByName("souffle_sol/lambert3");
	if (!solTxt.isNull())
	{
		Real const curTime = m_Timer.getSeconds();
		
		Real alpha = std::min(std::max((curTime - 10.0f) / 60.0f, 0.0f), 1.0f);
		solTxt->setDiffuse(1, 1, 1, alpha);
	}

	m_lastTime = m_Timer.getSeconds();

	return (true);
}


void CSceneSouffle :: processEvents(void)
{
	CSceneBase::processEvents();

	m_Play = false;
	for (TEvents::iterator It = m_Events.begin(); It != m_Events.end(); ++It)
	{
		switch (It->m_Packet.eventid)
		{
			case SOpenMoveDisplayPacket::e_SE_MoveSign:
			{
				m_Play = true;
				break;
			}
		}
	}

	if (m_Timer.getSeconds() < 45.03f)
	{
		m_Play = true;
	}
}